﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OrganizedIN.BAL;
using OrganizedIN.BussinessServices;
using System.Drawing;

namespace OrganizedIN.UI.MyProfile
{
    public partial class myprofile : System.Web.UI.Page
    {
        OIDataContext dc = new OIDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (Request["UID"] == null)
                    {
                        fillInformation(Session["UserID"].ToString(), true);
                    }
                    else
                    {
                        fillInformation(Request["UID"], false);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void fillInformation(string username, bool controlvisibility)
        {
            var gsinfo = (from d in dc.UserInformations
                          where d.username == username
                          select d).SingleOrDefault();
            txtemail.Text = gsinfo.username;
            txtName.Text = gsinfo.FirstName;
            txtLastName.Text = gsinfo.LastName;
            ddlselectgender.Text = gsinfo.Gender;
            txtdob.Text = gsinfo.DOB;
            ddlselecttimezone.Text = gsinfo.Time_Zone;
            txtemail.Enabled = false;
            if (!controlvisibility)
            {
                txtemail.BorderStyle = BorderStyle.None;
                txtName.BorderStyle = BorderStyle.None;
                txtdob.BorderStyle = BorderStyle.None;
                txtLastName.BorderStyle = BorderStyle.None;
                ddlselectgender.Enabled = false;
                ddlselecttimezone.Enabled = false;
                btn_Save.Visible = false;
                txtemail.BackColor = Color.Transparent;
                txtName.BackColor = Color.Transparent;
                txtdob.BackColor = Color.Transparent;
                txtLastName.BackColor = Color.Transparent;
                txtdob.Enabled = false;
                txtLastName.Enabled = false;
                txtName.Enabled = false;
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtemail.Text != "" && txtName.Text != "")
                {
                    string username = Session["UserID"].ToString();
                    var gsinfo = (from d in dc.UserInformations
                                  where d.username == username
                                  select d).SingleOrDefault();
                    gsinfo.FirstName = txtName.Text;
                    gsinfo.LastName = txtLastName.Text;
                    gsinfo.DOB = txtdob.Text;
                    gsinfo.Time_Zone = ddlselecttimezone.Text;
                    gsinfo.Gender = ddlselectgender.Text;
                    clearAll();
                    dc.SubmitChanges();
                    lblerrmsg.Text = "Update Successfully..";
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void clearAll()
        {
            txtdob.Text = "";
            txtemail.Text = "";
            txtLastName.Text = "";
            txtName.Text = "";
        }
    }
}
